using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.Serialization;

namespace MiniStock.Entities.Common.Events
{
    [DataContract]
    [KnownType("GetKnownTypes")]
    public abstract class EventBase
    {
        [DataMember(Order=0)]
        public Guid EventId { get; set; }

        [DataMember(Order=1)]
        public DateTime EventDate { get; set; }

        [DataMember(Order = 2)]
        public string SenderAddress { get; set; }

        //[DataMember(Order = 3)]
        //public string OutQueueAddress { get; set; }

        //public string SubmittedBy { get; set; }

        [DataMember(IsRequired=false,Order=4)]
        public Guid? CorrelatedEventId { get; set; }

        public T GetCorrelatedEvent<T>()
            where T: EventBase, new()
        {
            return new T
                       {
                           EventId = Guid.NewGuid(),
                           CorrelatedEventId = this.EventId,
                           EventDate = DateTime.Now,
                       };
         }

        public T GetCorrelatedResultEvent<T>(EntityBase entity)
            where T : ResultEvent, new()
        {
            var result = GetCorrelatedEvent<T>();
            result.Entity = entity;
            return result;
        }


        public static IEnumerable<Type> GetKnownTypes()
        {
            List<Type> result = new List<Type>();
            result.Add(typeof(EntityEventBase));
            result.Add(typeof(ProtocolEventBase));
            result.AddRange(SerializationHelper.KnownEventTypes);

            Trace.TraceInformation("EventBase.GetKnownTypes: {0}", result.Count);
            return result;
        }
    }
}
